package com.voucher.qiege.one;

import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginService {

    public static void login(String username, String password, String liuchenghao) throws Exception {
        SessionManager.initSession();
        CloseableHttpClient client = SessionManager.getClient();
        CookieStore cookieStore = SessionManager.cookieStore;

        String encryptedPwd = PasswordEncryptor.encrypt(password, cookieStore);
        HttpPost loginReq = new HttpPost("http://oa.mexingroup.com:9999/j_acegi_security_check");
        loginReq.setEntity(new UrlEncodedFormEntity(List.of(
                new BasicNameValuePair("j_username", username),
                new BasicNameValuePair("j_password", encryptedPwd)
        ), "UTF-8"));

        try (CloseableHttpResponse res = client.execute(loginReq)) {
            String content = EntityUtils.toString(res.getEntity());
            if (content.contains("location.href") && content.contains("sys/portal/page.jsp")) {
                System.out.println("登录成功！Cookies:\n ");
//                System.out.println(cookieStore.getCookies());

            } else if (content.contains("用户名或密码不正确") || content.contains("valida ukeycode error")) {
                throw new RuntimeException("登录失败：用户名或密码不正确");
            } else {
                throw new RuntimeException("登录失败：未知错误");
            }

        }
        String downloadUrl = "http://oa.mexingroup.com:9999/km/review/km_review_main/kmReviewMain.do"
                + "?method=view"
                + "&fdId=" + URLEncoder.encode(liuchenghao, "UTF-8");
        HttpGet downloadRequest = new HttpGet(downloadUrl);
        CloseableHttpResponse response = client.execute(downloadRequest);
//        String html = EntityUtils.toString(response.getEntity());
        String html = removeSpacesAndNewlines(EntityUtils.toString(response.getEntity()));
        System.out.println(html);
        System.out.println("------------------");
        List<String> ids = extractIds(html);
        for (String id : ids) {
            System.out.println(id);
        }
    }
    public static String removeSpacesAndNewlines(String input) {
        // 去除空格和换行符
        return input.replaceAll("\\s+", "");
    }
    public static List<String> extractIds(String html) {
        List<String> ids = new ArrayList<>();
        // 定义正则表达式
        String regex = "<tr id=\"([^\"]{32})";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(html);

        // 查找所有匹配的 id
        while (matcher.find()) {
            ids.add(matcher.group(1));
        }
        return ids;
    }
}
